package com.kcht.itsed.mws.subscribescheduler.comm;

import com.kcht.itsed.mws.alemodel.ECReports;
import com.kcht.itsed.mws.subscribescheduler.biz.IConcreteReportSender;
import com.kcht.itsed.mws.subscribescheduler.facade.IPollManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import java.io.InputStream;

/**
 * 同步订阅发送器
 * 对于ale.poll接口，使用此报告发送器进行发送
 * 对应的url格式为:
 * 	poll://ec/ecSpecName
 * 目前尚未考虑对同一规则并发Poll的情况，以后再说
 * 
 * 
 * 发送报告直接到
 * @author zhangwenshuo
 *
 */
@Component("PollReportSender")
@Slf4j
public class PollReportSender implements IConcreteReportSender{

	@Autowired
	IPollManager pollManager;

	@Override
	public void send(InputStream reportIS) throws ReportSendFailException {
		try {
			JAXBContext context = JAXBContext.newInstance(ECReports.class);
			Unmarshaller shaller = context.createUnmarshaller();
			JAXBElement<ECReports> root = shaller.unmarshal(new StreamSource(reportIS), ECReports.class);
			ECReports value = root.getValue();
			pollManager.notifyECPollResult(value.getSpecName(), value);
		} catch (JAXBException e) {
			log.error("",e);
		}
	}

	@Override
	public ReportSendingState getSendingState() {
		return ReportSendingState.SENDING;
	}
	
}
